angular.module('BatchBind')
    .controller('BatchBind.BatchBindCtrl', ['$scope', '$state', '$http', '$ionicLoading', '$ionicPopup', 'Http',
        function($scope, $state, $http, $ionicLoading, $ionicPopup, Http) {
            $scope.moLotNumbers = [
                { id: '001' },
                { id: '002' },
                { id: '003' },
                { id: '004' }
            ];
            $scope.data = {
                laserId: '',
                moLotNumber: '006',
                barcodeId: ''
            };
            $scope.clearInput = function(value) {
                if (value === 'laserId') {
                    $scope.data.laserId = '';
                } else if (value === 'molot') {
                    $scope.data.molot = '';
                }
            };

            $scope.Barcode = 'Item00000001';
            $scope.init = function(e) {
                getQcInfo($scope.Barcode);
            };

            $scope.submit = function() {
                if (!$scope.data.laserId || $scope.data.laserId === '') {
                    alert('请扫描镭射标');
                    return;
                }
                $ionicLoading.show();
                Http.post({
                    url: '/api/ProductFlow/MOLotNumberToAssemblyBarcode',
                    data: {
                        Parameter: {
                            id: $scope.Barcode,
                            securityId: $scope.data.laserId,
                            moLotNumber: $scope.data.moLotNumber
                        }
                    }
                }).then(function() {
                    $ionicLoading.hide();
                    showConfirm('提交成功', '为了保证程序安全，如不再需要修改或继续，请手动退出');
                }, function(err) {
                    $ionicLoading.hide();
                })['finally'](function() {
                    $ionicLoading.hide();
                });
            };

            function getQcInfo(Barcode) {
                $ionicLoading.show();
                Http.post({
                    url: '/api/Wrapper/getWrapperInfo',
                    data: {
                        Parameter: {
                            Barcode: Barcode,
                            TerminalUniqueID: '11:33:55:77'
                        }
                    }
                }).then(function(data) {
                    $scope.barcode = data.basicInfo;
                    $scope.qcStandard = data.qcStandard;
                }, function(err) {

                })['finally'](function() {
                    $ionicLoading.hide();
                });
            }

            function showConfirm(title, temp) {
                var confirmPopup = $ionicPopup.alert({
                    title: title,
                    template: temp
                });
                confirmPopup.then(function(res) {
                    if (res) {

                    } else {

                    }
                });
            }
            $scope.init();
        }
    ]);
